from django.db import models


''''普通用户'''
class OrdinaryUser(models.Model):
    user_name = models.CharField(max_length=32, verbose_name='账号')
    password = models.CharField(max_length=32, verbose_name='密码')
    email = models.EmailField(verbose_name='邮箱')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')


    @classmethod
    def get_user_status(cls, user_name):  # 看状态是否存在
        return cls.objects.filter(user_name=user_name)

    @classmethod
    def create_new_user(cls, user_name, password, email):
        return cls.objects.create(user_name=user_name, password=password, email=email)  # 此操作是对于django而言的

    @classmethod
    def test_user_and_password(cls, user, password):   # 查看是否相等
        context = {}
        # 判断用户是否存在
        if cls.objects.filter(user_name=user):
            context['user'] = 1
        else:
            context['user'] = -1
            return context   # 如果用户名出错，那么直接返回账户出错
        # 判断用户的账户和密码是匹配
        if cls.objects.filter(user_name=user, password=password):
            context['password'] = 1
        else:
            context['password'] = -1
            return context
        return True

    class Meta:
        verbose_name = verbose_name_plural = '普通用户'

    def __str__(self):
        return self.user_name